package com.your.netty.sample.main;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.your.netty.sample.core.Application;
import com.your.netty.sample.server.YourServerInitHandler;

public class NettyJavaSampleRunner {
	private static final Logger logger = Logger.getLogger(NettyJavaSampleRunner.class);
	static int PORT = 8081;
	static {
		Application.ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		Application.properties = (Properties) Application.ctx.getBean("reckonProperties");
	}

	public static void main(String[] args) throws InterruptedException {
		// Configure the server.
		EventLoopGroup bossGroup = new NioEventLoopGroup(Integer.parseInt(Application.properties
				.getProperty("bossGroup")));
		EventLoopGroup workerGroup = new NioEventLoopGroup(Integer.parseInt(Application.properties
				.getProperty("workerGroup")));
		try {
			ServerBootstrap b = new ServerBootstrap();
			b
					.group(bossGroup, workerGroup)
					.channel(NioServerSocketChannel.class)
					.handler(new LoggingHandler(LogLevel.INFO))
					.childHandler(new YourServerInitHandler());

			PORT = Integer.valueOf(Application.properties.getProperty("http.port"));
			Channel ch = b.bind(PORT).sync().channel();
			ch.closeFuture().sync();
		} catch (Exception e) {
			logger.error("服务启动异常", e);
		} finally {
			bossGroup.shutdownGracefully();
			workerGroup.shutdownGracefully();
		}
	}
}
